package cn.com.service.impl.sys;

import static cn.com.support.message.MessageCodeEnum.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import cn.com.commons.Paginator;
import cn.com.dal.dao.sys.DictDAO;
import cn.com.dal.dataobject.hr.DeptDO;
import cn.com.dal.dataobject.sys.DictDO;
import cn.com.service.sys.DictService;
import cn.com.support.message.MessageResult;

/**
 * 系统字典SERVICE实现类
 * @author Zoe
 */
@Service
public class DictServiceImpl implements DictService {

	private Logger logger = LoggerFactory.getLogger(DictServiceImpl.class);

	@Autowired
	private DictDAO<DictDO> dictDAO;

	@Override
	public <C> Paginator<C, List<DictDO>> selectByPage(
			Paginator<C, List<DictDO>> paginator) {
		Paginator<C, List<DictDO>> result = null;
		try {
			result = dictDAO.selectForPage(paginator);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]",
							paginator.getCondition()), e);
		}
		//return json string
		return result;
	}

	@Override
	public MessageResult<Long> insert(DictDO dictDO) {
		MessageResult<Long> result =  new MessageResult<Long>(false, null, ERROR_INSERT);
		try {
			Long id = dictDAO.insert(dictDO);
			if (id != null && id > 0) {
				result = new MessageResult<Long>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			logger.error(String.format(ERROR_INSERT.descript() + ":[%s]", dictDO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> updateById(DictDO dictDO) {
		MessageResult<Integer> result =  new MessageResult<Integer>(false, null, ERROR_UPDATE);
		try {
			Integer id = dictDAO.updateById(dictDO);
			if (id != null && id > 0) {
				result = new MessageResult<Integer>(true, id, INFO_SUCCEED);
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_UPDATE.descript() + ":[%s]", dictDO), e);
		}
		return result;
	}

	@Transactional
	@Override
	public MessageResult<Integer> deleteById(List<String> idLst) {
		MessageResult<Integer> result =  new MessageResult<Integer>(true, null, INFO_SUCCEED);
		Integer opId = null;
		try {
			for(String id : idLst){
				opId = dictDAO.deleteById(Long.parseLong(id));
				if (opId == null || opId <= 0) {
					result = new MessageResult<Integer>(false, opId, ERROR_DELETE);
					break;
				}
			}
		} catch (Exception e) {
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			logger.error(String.format(ERROR_DELETE.descript() + ":[%s]", idLst), e);
		}
		return result;
	}

	@Override
	public List<DictDO> selectList(DictDO dictDO) {
		List<DictDO> result = null;
		try {
			result = dictDAO.selectList(dictDO);
		} catch (Exception e) {
			logger.error(
					String.format(ERROR_SELECT.descript() + ":[%s]"), e);
		}
		return result;
	}

}
